package com.zimmur.service.impl.zone;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.common.ConstData;
import com.zimmur.dao.zone.ITopicRecommandDao;
import com.zimmur.model.account.GoodsUser;
import com.zimmur.model.zone.TopicRecommand;
import com.zimmur.service.IUserService;
import com.zimmur.service.RedisCacheUtil;
import com.zimmur.service.impl.base.AdBannerServiceImpl;
import com.zimmur.service.zone.ITopicRecommandService;
@Service("topicRecommandService")  
public class TopicRecommandServiceImpl implements ITopicRecommandService{
	private static Logger log=LoggerFactory.getLogger(AdBannerServiceImpl.class);
	
	@Resource  
    private ITopicRecommandDao topicRecommandDao;
	@Resource
	private RedisCacheUtil<TopicRecommand> stringRedisDao;
	@Resource
	private IUserService userService;

	private final String key ="zm_topic_recommand";
	
	private static Object syncLock = new Object();
	
	@Override
	public List<GoodsUser> selectTopicRecommandList3(Short typeFlag,long createUserId) {
		List<TopicRecommand> topicRecommandList=selectTopicRecommandAll(typeFlag);
		Integer total=topicRecommandList.size();
		List<GoodsUser> recommandUsers=new ArrayList<>();
		if(total>3){
			int i=(int)(Math.random()*(total-2));
			for(int j=0;j<3;j++){
				GoodsUser user =userService.selectTopicIsConcern(createUserId,topicRecommandList.get(i+j).getUserId(),typeFlag.intValue());
				if(user != null){
	    			recommandUsers.add(user);
	    		}
			}
		}else{
			for(TopicRecommand recommand:topicRecommandList){
				GoodsUser user =userService.selectTopicIsConcern(createUserId,recommand.getUserId(),typeFlag.intValue());
				if(user != null){
	    			recommandUsers.add(user);
	    		}
			}	
		}
		return recommandUsers;
	}
	private List<TopicRecommand> selectTopicRecommandAll(Short typeFlag){
		String redisKey = key +"_"+typeFlag+ "selectTopicRecommandAll";
		List<TopicRecommand> topicRecommandList=null;
		try {
			//获取 最大值
			boolean bRedis = false;
			try {
				bRedis = stringRedisDao.exists(redisKey);
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			if(bRedis){
				//从缓存中读取
				topicRecommandList = stringRedisDao.getCacheList(redisKey);
			}else{
				synchronized (syncLock) {
					try {
						bRedis = stringRedisDao.exists(redisKey);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
					if(bRedis){
						//从缓存中读取
						topicRecommandList = stringRedisDao.getCacheList(redisKey);
					}else{
						/*缓存中不存在，创建后存入*/
						//从数据库读取
						topicRecommandList = topicRecommandDao.selectTopicRecommandListAll(typeFlag);
						try {
							//存入缓存,过期时间10分钟
							stringRedisDao.setCacheList(redisKey,topicRecommandList);
							stringRedisDao.setTimeout(redisKey, ConstData.REDIS_KEY_TIMEOUT);
						} catch (Exception e) {
							log.error(e.getMessage());
						}
					}
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return topicRecommandList;
	}

}
